package com.allwees.bs.core.modelbase.branch;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.allwees.bs.core.modelbase.exception.AuthExceptionEnum;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.util.Assert;

import java.io.Serializable;

/**
 * <ul>BranchUser描述了与分支机构有关联关系的User，BranchType包括：
 * 		<li>{@link EBranchType#PLATFORM}</li>
 * 		<li>{@link EBranchType#SUPPLIER}</li>
 * 		<li>{@link EBranchType#RETAILER}</li>
 * 		<li>{@link EBranchType#STORE}</li>
 * </ul>
 *
 * @author yangylsky
 * @since 2019-09-25
 *
 */
public interface BranchUser extends Serializable {
	boolean isEnabled();

	EBranchType getBranchType();

	default String[] getRoleNames() {
		return ArrayUtils.EMPTY_STRING_ARRAY;
	}

	/**
	 * 唯一编号
	 *
	 * @return
	 */
	String getUuid();

	/**
	 * 关联的分支机构uuid
	 *
	 * @return
	 */
	String getBranchUuid();

	@JsonIgnore
	default String getSupplierUuid() {
		return getCurrentSupplierUuid();
	}

	@JsonIgnore
	default String getRetailerUuid() {
		return getCurrentRetailerUuid();
	}

	@JsonIgnore
	default String getStoreUuid() {
		return getCurrentStoreUuid();
	}

	@JsonIgnore
	default String getPlatformUuid() {
		return getCurrentPlatformUuid();
	}

	default String checkSupplierUuid() {
		String supplierUuid = getSupplierUuid();
		Assert.notNull(supplierUuid, AuthExceptionEnum.NO_BRANCH_ID.getMsg());
		return supplierUuid;
	}

	default String checkRetailerUuid() {
		String retailerUuid = getRetailerUuid();
		Assert.notNull(retailerUuid, AuthExceptionEnum.NO_BRANCH_ID.getMsg());
		return retailerUuid;
	}

	default String checkStoreUuid() {
		String storeUuid = getStoreUuid();
		Assert.notNull(storeUuid, AuthExceptionEnum.NO_BRANCH_ID.getMsg());
		return storeUuid;
	}

	default String checkPlatformUuid() {
		String platformUuid = getPlatformUuid();
		Assert.notNull(platformUuid, AuthExceptionEnum.NO_BRANCH_ID.getMsg());
		return platformUuid;
	}

	@JsonIgnore
	default String getCurrentSupplierUuid() {
		if (EBranchType.SUPPLIER.equals(getBranchType())) {
			return getBranchUuid();
		}
		return null;
	}

	@JsonIgnore
	default String getCurrentRetailerUuid() {
		if (EBranchType.RETAILER.equals(getBranchType())) {
			return getBranchUuid();
		}
		return null;
	}

	@JsonIgnore
	default String getCurrentStoreUuid() {
		if (EBranchType.STORE.equals(getBranchType())) {
			return getBranchUuid();
		}
		return null;
	}

	@JsonIgnore
	default String getCurrentPlatformUuid() {
		if (EBranchType.PLATFORM.equals(getBranchType())) {
			return getBranchUuid();
		}
		return null;
	}
}
